#include<iostream>
using namespace std;
struct People {
	int a, b, k;
};
int main()
{
	struct People* p;
	int x=0;
	int count=0;
	int **order;
	int n, m;
	int a, b, k;

	cin >> n >> m;

	p = new struct People[m];
	for (int i = 0; i < m; i++)
	{
		cin >> p[i].a >> p[i].b >> p[i].k;
		if (p[i].k == 1) count++;
		if (p[i].a == p[i].b)
		{
			cout << "inconsistent" << endl;
			return 0;
		}
	}
	order = new int*[count];
	int* y = new int[count];
	for (int i = 0; i < 10; i++) order[i] = new int[count+1];
	for (int i = 0; i < m; i++)
	{
		if (p[i].k == 1)
		{
			order[x][0] = p[i].a;
			order[x][1] = p[i].b;
			y[x] = 1;
			x++;
		}
	}

	for (int i = 0; i < count; i++)
	{
		for (int j = 0; j < count; j++)
		{
			if (j == i) continue;
			if (order[j][0] == order[i][y[i]])
			{
				for (int l = 1; l <= y[j]; l++)
				{
					y[i]++;
					order[i][y[i]] = order[j][l];
				}
				j = 0;
			}
		}
	}

	for (int i = 0; i < count; i++)
	{
		for (int j = i+1; j <count; j++)
		{
			if (y[i] < y[j])
			{
				int* temp = new int[count + 1];
				int temp1;
				temp = order[i];
				temp1 = y[i];
				order[i] = order[j];
				y[i] = y[j];
				order[j] = temp;
				y[j] = temp1;
			}
		}
	}	
	for (int i = 0; i < count; i++)
	{
		if (y[i] == 0) continue;
		for (int j = i+1; j <count; j++)
		{
			if (y[j] != 0)
			{
				int c = 0;
				for (int l = 0; j <= y[i]; l++)
				{
					if (order[j][c] == order[i][l])
					{
						c++;
						l = 0;
					}
					if (c > y[j]) break;
				}
				if (c == y[j] + 1)
				{
					for (int t = 0; t < count + 1; t++)
						order[j][t] = 0;
					y[j] = 0;
				}
			}
		}
	}

	for (int i = 0; i < count; i++)
	{
		if (y[i] != 0)
		{
			for (int j = 0; j <= y[i]; j++) cout << order[i][j] << " ";
			cout << endl;
		}
	}
	return 0;
}